package com.theory.bind;

import java.util.HashMap;
import java.util.Map;

public class MapperRegistry {
    private final Map<Class<?>, MapperProxyFactory<?>> knownMapperMap = new HashMap<>();

    public void registerMapper(Class<?> type){
        if(type.isInterface()){
            if(hasMapper(type)){
                throw new RuntimeException(type + " has already existed in MapperRegistry! ");
            }
            else{
                knownMapperMap.put(type, new MapperProxyFactory<>(type));
            }
        }
    }

    public boolean hasMapper(Class<?> type) {
        return knownMapperMap.containsKey(type);
    }

    @SuppressWarnings("unchecked")
    public <T> T getMapper(Class<T> type){
        MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMapperMap.get(type);
        return mapperProxyFactory.newInstance();
    }


}
